#pragma once

#include<vector>
#include<iostream>

using namespace std;

template<class k>
struct Hashfunc
{
	size_t operator()(const k& _kv)
	{

		return (size_t)_kv;
	}
};

template<>
struct Hashfunc<string>
{
	size_t operator()(const string& _kv)
	{
		size_t a = 0;
		for (auto e : _kv)
		{
			a += e;
			a *= 31;
		}
		return a;
	}
};

namespace bit
{

	template<class T>
	struct HashNode
	{
		T _kv;
		HashNode<T>* _next;



		HashNode(const T& kv)
			: _kv(kv)
			,_next(nullptr)
		{ }
	};

	template<class k, class T, class KeyofT, class Hash>
	class HashTable;

	template<class k, class T, class ref, class ptr, class KeyofT, class Hash>
	class IteratorHash
	{
		typedef HashNode<T> Node;
		typedef IteratorHash<k, T, ref, ptr, KeyofT, Hash> self;

		Node* _node;
		const HashTable<k, T, KeyofT, Hash>* _tbptr;
	public:
		IteratorHash(Node* node, const HashTable<k, T, KeyofT, Hash>* tbptr)
			:_node(node)
			,_tbptr(tbptr)
		{ }


		ref operator*()
		{
			return _node->_kv;
		}

		ptr operator->()
		{
			return &_node->_kv;
		}

		bool operator!=(self& s)
		{
			return s._node != _node;
		}

		self& operator++()
		{
			if (_node->_next != nullptr)
			{
				_node = _node->_next;
			}
			else
			{
				KeyofT kot;
				Hash ht;
				size_t hashi = ht(kot(_node->_kv)) % _tbptr->_tables.size();
				hashi++;
				while (hashi < _tbptr->_tables.size() && _tbptr->_tables[hashi] == nullptr)
				{
					hashi++;
				}
				if (hashi == _tbptr->_tables.size())
				{
					_node = nullptr;
				}
				else
				{
					_node = _tbptr->_tables[hashi];
				}
			}
			return *this;
		}

	};


	template<class k, class T, class KeyofT, class Hash>
	class HashTable
	{
		typedef HashNode<T> Node;
	public:
		template<class k, class T, class ref, class ptr,class KeyofT, class Hash>
		friend class IteratorHash;
		typedef IteratorHash<k, T, T&, T*, KeyofT, Hash> Iterator;
		typedef IteratorHash<k, T, const T&, const T*, KeyofT, Hash> ConstIterator;

		HashTable()
		{
			_tables.resize(10, nullptr);
		}

		~HashTable()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				while (_tables[i])
				{
					Node* root = _tables[i]->_next;
					delete _tables[i];
					_tables[i] = root;
				}
				_tables[i] = nullptr;
			}
		}

		Iterator Begin()
		{
			if (_n == 0) return Iterator(nullptr, this);
			else
			{
				int i = 0;
				while (_tables[i] == nullptr)
				{
					i++;
				}
				return Iterator(_tables[i], this);
			}
		}

		ConstIterator Begin()const
		{
			if (_n == 0) return ConstIterator(nullptr, this);
			else
			{
				int i = 0;
				while (_tables[i] == nullptr)
				{
					i++;
				}
				return ConstIterator(_tables[i], this);
			}
		}

		Iterator End()
		{
			return Iterator(nullptr, this);
		}		
		
		ConstIterator End()const
		{
			return ConstIterator(nullptr, this);
		}

		pair<Iterator, bool> Insert(const T& kv)
		{
			Hash hot;
			KeyofT kot;
			if (_n == _tables.size())
			{
				vector<Node*> _newtables(_tables.size() * 2, nullptr);
				for (size_t i = 0; i < _tables.size(); i++)
				{
					while(_tables[i])
					{
						Node* root = _tables[i]->_next;
						int hashi = hot(kot(_tables[i]->_kv)) % (_tables.size()*2);
						_tables[i]->_next = _newtables[hashi];
						_newtables[hashi] = _tables[i];
						_tables[i] = root;
					}
				}
				_tables.swap(_newtables);
			}
			int hashi = hot(kot(kv)) % _tables.size();
			Node* root = _tables[hashi];
			_tables[hashi] = new Node(kv);
			_tables[hashi]->_next = root;
			_n++;
			return { Iterator(_tables[hashi], this), true};
		}

		Iterator Find(const k& key)
		{
			Hash hot;
			KeyofT kot;
			int hashi = hot(key) % _tables.size();
			Node* root = _tables[hashi];
			while (root)
			{
				if (kot(root->_kv) == key)
				{
					return Iterator(root, this);
				}
				root = root->_next;
			}
			return Iterator(nullptr, this);
		}

		bool Erase(const k& key)
		{
			Hash hot;
			KeyofT kot;
			int hashi = hot(key) % _tables.size();
			Node* root = _tables[hashi];
			Node* prev = nullptr;
			while (root)
			{
				if (kot(root->_kv) == key)
				{
					if (prev == nullptr)
					{
						_tables[hashi] = root->_next;
					}
					else
					{
						prev->_next = root->_next;
					}
					delete root;
					_n--;
					return true;
				}
				prev = root;
				root = root->_next;
			}
			return false;
		}


	private:
		vector<Node*> _tables;
		size_t _n = 0;
	};
}
